;Code Some OS instruction to test
;simple,simple.
;you can add what you want here

  format mz
	include "../common/pminc.asm"


macro defd descript,name,msize
{
 str#name db descript,0  ;string
 if msize eq
   dw 4
  m#name dd 0       ;content
 else
   dw msize
   m#name:
   repeat msize
    db 0
   end repeat
 end if
}

macro dump addr
{
  mov   si,addr
  call  dispC      
}

;GDT segment
  .GDT
  DUMMY_DESC
  normal  MZDESC    0,     0xffff,      SDT_DATA_RW
  video   MZDESC    0b800h,7fffh,       SDT_DATA_RW
  tc16    MZDESC    tc,    0xffff,      SDT_CODE_RE
  td16    MZDESC    rdat,  rdat.limit,  SDT_DATA_RW

;tmp code segment
  .code tc
enter_pmode16:
  mov     ax,td16.sel
  mov     ds,ax
  mov     ax,video.sel
  mov     es,ax

  sgdt    fword [mGDTR]
  dump    strGDTR
  
  sidt    fword [mIDTR]
  dump    strIDTR
  
  smsw    word [mMSW]
  dump    strMSW 

  str     word [mTR]
  dump    strTR

  sldt    word [mLDTR]
  dump    strLDTR

  mov     eax,cr0
  mov     [mCR0],eax
  dump    strCR0

  mov     eax,cr3
  mov     [mCR3],eax
  dump    strCR3

  mov     eax,dr7
  mov     [mDR7],eax
  dump    strDR7

  ;anything need to test may place here
  ;....
  ;....

  mov ax,normal.sel
  mov ds,ax
  mov es,ax
  mov fs,ax
  mov gs,ax
  mov ss,ax
  ClrCr0Pebit
  jmp main:ret_at_dos
dispC:
  mov   di,[lx]
  inc   [lx]
  imul  di,di,80*2
  add   di,[cy]
@@:
  lodsb
  or   al,al
  jz   @F
  mov  ah,4fh
  stosw
  jmp  @B
@@:
  lodsw
  mov     cx,ax
  cmp     cx,2
  jne     @@4 
  call    dispWORD
  jmp     @exitSUBp
@@4:
  cmp     cx,4
  jne     @@6
  call    dispDWORD
  jmp     @exitSUBp
@@6:
  cmp     cx,6
  jne     @exitSUBp
  call    disp6bytes
@exitSUBp:
  ret

dispWORD:
  lodsw
  mov     dx,ax
  shl     cx,1    ;*2
@@:
  mov     ax,dx
  rol     ax,4
  mov     dx,ax
  b2Asc
  mov     ah,4fh
  stosw
  loop    @B
  ret
dispDWORD:
  lodsd
  mov     edx,eax
  shl     cx,1    ;*2
@@:
  mov     eax,edx
  rol     eax,4
  mov     edx,eax
  b2Asc
  mov     ah,4fh
  stosw
  loop    @B
  ret
disp6bytes:
  mov     cx,2
  call    dispWORD
  mov     eax,0x00200020
  stosd
  mov     cx,4
  call    dispDWORD
  ret
 

;data segment
  .data rdat

Vgdtr   XDTR GDTSEG.limit,0
orgSP   dw ?
orgSS   dw ?

lx      dw 4
cy      dw 0
defd "The content in MSW is:",MSW,2
defd "The content in Gdtr is:",GDTR,6
defd "The content in IDTR is:",IDTR,6
defd "The content in TR is:",TR,2
defd "The content in LDTR is:",LDTR,2
defd "The content in CR0 is:",CR0
defd "The content in CR3 is:",CR3
defd "The content in DR7 is:",DR7

  ;initializing segment
  .code main
start:
	mov     ax,GDTSEG
  mov     ds,ax
  
  ;init GDT
  mov     si,0
  mov     cx,GDTSEG.items
  call    ..init_xdt

  ;init GDTR
  mov     ax,rdat
  mov     ds,ax
  mov     eax,GDTSEG
  shl     eax,4
  shld    edx,eax,16
  mov     word [Vgdtr.base_lo],ax
  mov     word [Vgdtr.base_hi],dx
  
  mov     word [orgSP],SP
  mov     word [orgSS],SS
  
  cli     
  lgdt    [Vgdtr]
  EnableA20
  SetCr0Pebit
  jmp     tc16.sel:enter_pmode16
ret_at_dos:
  mov     ax,rdat
  mov     ds,ax
  lss     sp,dword [orgSP]
  sti

  mov     ax,4c00h
  int     21h

  init_xdt_proc

.end main:start

